﻿using ScFramework.Core;
using System;
using System.Collections;
using UnityEngine;

public class ElementParser : LogicModuleBase<ScFramework.Core.Data>
{
    public override void OnEnable()
    {
        base.OnEnable();
        var reg=GetInnerModule<ICollisionHandlerRegister>();
        ElementState Stressing=0, Stressed=0;
        reg.RegisterCertainTypeHandler(Stressing,Stressed,(CollisionArgs args,ISharedCollisionManager m) =>
          {
              
              //BasicInfo StressingMessage = m.GetInfo(args.StressingSystem,args.StressingID);
              //BasicInfo StressedMessage = m.GetInfo(args.StressedSystem, args.StressedID);
              string  StressingName = args.StressingState.ToString();
              string StressedName = args.StressedState.ToString();
              string[] functions = SysModule.Table.GetStrings("ReactionSystem", StressingName,StressedName);

              switch (functions.Length)
              {
                  case 0:
                      break;
                  case 1:
                      m.Damage(args.StressedSystem, args.StressedID, GetDamageInt(functions[0]));
                      break;
                  case 2:
                      m.Damage(args.StressedSystem, args.StressedID, GetDamageInt(functions[1]));
                      m.Create(args.StressedSystem, args.StressedState, m.GetInfo(args.StressedSystem, args.StressedID).Position);
                      break;

              }
              
              m.GetHandler(args.StressingState, args.StressedState)(args,m);
              
          });
        


        

    }

    
    
   /* private int TextResolver(string row,string column)//0返回无反应，1返回伤害，2返回伤害+摧毁生成
    {
        string s=SysModule.Table.GetString("ReactionSystem", row, column);
        if (s == null || s.Length == 0)
        {
            return 0;
        }
        else
        {
            string[] Functions = s.Split('|');
            return Functions.Length;
        }
        
    }*/

    private int StringToInt(string value)
    {
        int n = 0;
        if (value == "inf")
        {
            n= Int32.MaxValue;
        }
        else
        {
            for(int i = 0; i < value.Length; i++)
            {
                n *= 10;
                n += (value[i] - '0');
            }
        }
        return n;
    }

    private int GetDamageInt(string D)
    {
        string s = string.Empty;
        if (D.StartsWith("D("))
        {
            s=D.Substring(2, D.Length - 3);
        }
        else
        {
            Debug.Log("伤害字符鉴定错误");
        }

        return StringToInt(s);
    }

    private int CreateNum(string C)
    {
        string s = string.Empty;
        if (C.StartsWith("C("))
        {
            for(int i = 2; C[i]!=','; i++)
            {
                s = s + C[i];
            }
        }
        else
        {
            Debug.Log("生成数量鉴定错误");
        }
        return StringToInt(s);
    }

    private ElementState GetCreateState(string C)
    {
        ElementState element = 0;
        string s = string.Empty;
        if (C.StartsWith("C("))
        {
            int i = 0;
            while (C[i] != ',')
            {
                i++;
            }
            for (i++; C[i] != ')'; i++)
            {
                s = s + C[i];
            }
            foreach (ElementState step in Enum.GetValues(typeof(ElementState))){
                if (step.ToString() == s)
                {
                    element = step;
                    break;
                }
            }
        }
        else
        {
            Debug.Log("生成数量鉴定错误");
        }
        return element;
    }

    
}

